#pragma once
#include <QDialog>
#include <QFrame>

#include "widgets.h"

class QHBoxLayout;
class SImageButton;
class QLabel;
class WidgetResizer;
class DlgTitleBar;
class S_WIDGETS_EXPORT SBaseDialog : public QFrame {
  friend class DlgTitleBar;
  Q_OBJECT
 public:
  enum class WindowType { kDialog, kFixedDialog, kChild };

  SBaseDialog(WindowType type = WindowType::kDialog, QWidget* parent = nullptr);
  ~SBaseDialog();

  void paintEvent(QPaintEvent* event) override;
  void showEvent(QShowEvent* event) override;

  void doModal();  // no block
  QDialog::DialogCode result() { return m_result; }
  void setWindowType(WindowType type);
  void setContentWidget(QWidget* w, bool web = false);
  QWidget* contentWidget() const { return m_contentWidget; }
  void removeContentWidget();
  void detach();

 signals:
  void accepted();
  void finished(int result);
  void rejected();
  void moving(SBaseDialog* dlg);

 public slots:
  void done(QDialog::DialogCode code);
  void reject() { done(QDialog::Accepted); }
  void setTitle(const QString& str);

 private:
  WindowType m_winType = WindowType::kDialog;
  bool m_webContent = false;
  QWidget* m_contentWidget = nullptr;
  DlgTitleBar* m_titleBar = nullptr;
  WidgetResizer* m_resizer = nullptr;
  QDialog::DialogCode m_result = QDialog::Rejected;
};

class DlgTitleBar : public QWidget {
  Q_OBJECT
 public:
  DlgTitleBar(SBaseDialog* parent);
  void setTitle(const QString& title);
  void dlgTypeChanged();
  void setWebDlg(bool v);

 protected:
  void mousePressEvent(QMouseEvent* event) override;
  // void mouseReleaseEvent(QMouseEvent *event) override;
  void mouseDoubleClickEvent(QMouseEvent* event) override;
  void mouseMoveEvent(QMouseEvent* event) override;

 private slots:
  void onBtnClicked();

 private:
  SBaseDialog* m_dlg = nullptr;

  QLabel* m_titleLabel = nullptr;
  SImageButton* m_btnReload = nullptr;
  SImageButton* m_btnPin = nullptr;
  SImageButton* m_btnPinSel = nullptr;
  SImageButton* m_btnFull = nullptr;
  SImageButton* m_btnRestore = nullptr;
  SImageButton* m_btnClose = nullptr;
  bool m_webDlg = false;
  QPoint m_dragPos;
};